﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZhongheCms.Models;
using GsnHelper;
namespace ZhongheCms.Controllers
{
    public class ShipLineController : Controller
    {
        //
        // GET: /ShipLine/

        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "",string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblShipLine.GetShioList( keywords, GswTblUser.CurrentUser.CustomerId, fid);

            return View(qry);
        }

        public ActionResult Create(int id = 0,int rid=0, string m = "", string f = "", string p = "", string fid = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var shipline =id==0? new GswTblShipLine{Lno = 1}:GswTblShipLine.SingleOrDefault(id);
            shipline.ShioRouteLines = GswTblShioRouteLine.Fetch("where ParentId=" + id + " order by Distance");
            if (id > 0 && rid == 0 && shipline.ShioRouteLines.Count>0)
                rid = shipline.ShioRouteLines[0].Id;
            shipline.ShioRouteLine=rid==0?new GswTblShioRouteLine{Sno = 1}:shipline.ShioRouteLine=GswTblShioRouteLine.SingleOrDefault(rid);
            if (id != 0) return View(shipline);
            var qry =
                GswTblShipLine.FirstOrDefault("select max(Lno) as Lno from  GswTblShipLine where CustomerId=" +
                                              GswTblUser.CurrentUser.CustomerId + " and FunctionId=" + fid);
            if (qry != null)
                shipline.Lno = qry.Lno + 1;
            //if (id > 0 && rid == 0)
            //{
            //    var qry1 =
            //    GswTblShioRouteLine.FirstOrDefault("select max(Sno) as Sno from  GswTblShioRouteLine ParentId="+id);    

            //}
            
            return View(shipline);

        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(int id,int rid, FormCollection c, string m = "", string f = "", string p = "", string fid = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            if (string.IsNullOrEmpty(c["OriginPlace"]))
                ModelState.AddModelError(string.Empty, "始发地不允许为空");
            if (string.IsNullOrEmpty(c["EndPlace"]))
                ModelState.AddModelError(string.Empty, "目的地不允许为空");
            if(InputValid.StrToInt(c["Lno"], 0)==0)
                ModelState.AddModelError(string.Empty, "线路序号为非法的数字格式");

            var shipline =id==0? new GswTblShipLine():GswTblShipLine.SingleOrDefault(id);
           
            shipline.OriginPlace = c["OriginPlace"];
            shipline.EndPlace = c["EndPlace"];
            shipline.TotalDistance = c["TotalDistance"];
            shipline.Manager = c["Manager"];
            shipline.Tel = c["Tel"];
            shipline.Lno = InputValid.StrToInt(c["Lno"], 0);
            shipline.LinePic = c["LinePic"];
            shipline.CustomerId = GswTblUser.CurrentUser.CustomerId;
            shipline.FunctionId = InputValid.StrToInt(fid,0);
             var routeLine = rid == 0 ? new GswTblShioRouteLine() : GswTblShioRouteLine.SingleOrDefault(rid);
            routeLine.RouteLine = c["RouteLine"];
            routeLine.Distance =InputValid.StrToInt(c["Distance"],0);
           // routeLine.Sno=InputValid.StrToInt(c["Sno"], 0);

            try
            {
                if (id == 0)
                {
                    shipline.Insert();
                }
                else
                {
                    shipline.Update();
                }
                if (!string.IsNullOrEmpty(c["RouteLine"]) && InputValid.StrToInt(c["Distance"],0)>0 )
                {
                    routeLine.ParentId = shipline.Id;
                    if (rid == 0)
                        routeLine.Insert();
                    else
                        routeLine.Update();
                    routeLine.Distance = 0;
                    routeLine.RouteLine = "";
                    routeLine.Id = 0;
                }
                else
                {
                    if (id == 0)
                    {

                        shipline.Id = 0;
                        shipline.OriginPlace = "";
                        shipline.EndPlace = "";
                        shipline.TotalDistance = "";
                        shipline.Manager = "";
                        shipline.Tel = "";
                        shipline.Lno++;
                        shipline.LinePic = "";
                    }
                   

                }

                ViewBag.scode = "1";
            }
            catch (Exception e)
            {
                ViewBag.scode = "0";
                ModelState.AddModelError(string.Empty, e.Message);
            }
            shipline.ShioRouteLines = GswTblShioRouteLine.Fetch("where ParentId=" + shipline.Id + " order by Distance");
            shipline.ShioRouteLine = routeLine;

            return View(shipline);
        }
        public ActionResult RouteDelete(int id,int rid, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblShioRouteLine.Delete(rid);
            var shipline =GswTblShipLine.SingleOrDefault(id);
            shipline.ShioRouteLines = GswTblShioRouteLine.Fetch("where ParentId=" + id + " order by Distance");
            shipline.ShioRouteLine = new GswTblShioRouteLine ();
            return View("Create", shipline);
        }
        public ActionResult Delete(int id, string m = "", string f = "", string p = "", string fid = "",  string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblShipLine.Delete("where Id=" + id);
            GswTblShioRouteLine.Delete("where ParentId=" + id);
            var qry = GswTblShipLine.GetShioList(keywords, GswTblUser.CurrentUser.CustomerId, fid);
            return View("Index", qry);
        }


    }
}
